// pages/myclublist/myclublist.js
const app = getApp()
const db = wx.cloud.database()
const _ = db.command
const user = db.collection('user')
const club = db.collection('club')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    joinclub: [],
    allclub: [],
    test: [],
    joinid: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const app = getApp()
    // console.log(app.globalData.openID)
    var _this = this

    /*
    user.get().then(res => {
      var cl = res.data[0].myclub
      for (var i = 0; i < cl.length; i++) {
        console.log(i)
        console.log(cl[i])
        club
          .doc(cl[i])
          .get()
          .then(res => {
            templist.push(res.data)
            _this.setData({
              joinclub: templist
            })
           templist.forEach((item)=>{
              console.log('item._id为：'+item._id)
            let joinid =[];
            joinid.push(item._id);
            _this.setData({
              joinid:joinid,
            }) })
            console.log('_this.data.joinid'+_this.data.joinid)
            club.where({
              _id: _.nin(_this.data.joinid)
            })
            .get()
            .then((res) => {
              _this.setData({
                allclub: res.data
              })
            })
          })
         }
         })         */

    user.where({
        _openid: app.globalData.openID
      }).get()
      .then((res) => {
        console.log('myclub值为：' + res.data[0].myclub)
        let myclub = res.data[0].myclub
        let clubchecking = res.data[0].clubchecking

        let bothclub = myclub.concat(clubchecking)

        club.where({
            _id: _.in(myclub)
          }).get()
          .then(res => {
            _this.setData({
              joinclub: res.data,
            })
          })

        club.where({
            _id: _.in(clubchecking)
          }).get()
          .then(res => {
            _this.setData({
              clubchecking: res.data,
            })
          })

        club.where({
            _id: _.nin(bothclub)
          }).get()
          .then(res => {
            _this.setData({
              allclub: res.data,
            })

          })

      })






  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})